<?php

class AuthController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	
    }

    public function loginAction()
    {
    	$form = new Application_Form_Login();
    	$this->view->form = $form;
    	$request = $this->getRequest();
        if($request->isPost()){
        	if($form->isValid($request->getPost())){
        		$messenger = $this->_helper->FlashMessenger;
				$messenger->addMessage('You successfully logged in');
				$this->_helper->redirector('success', 'index');
        	}
        	else{
        		//form data is INVALID
        	}        	
        }
        else{
        	//request is not POST
        }
    }
    public function registerAction()
    {
    	$form = new Application_Form_Register();
    	$this->view->form = $form;
    	$request = $this->getRequest();
        if($request->isPost()){
        	if($form->isValid($request->getPost())){
        		$password = $form->getValue('password');
        		$passwordConf = $form->getValue('passwordConf');
        		$name = $form->getValue('name');
        		if($password != $passwordConf)
        		{
        			$this->view->message = 'Passwords does not match';
        			$this->view->form->populate($request->getPost());
        			return;
        		}
        		$hashPass = sha1($password);
        		$user = new Application_Model_DbTable_User();
        		$user->addUser($form->getValue('name'), $form->getValue('email'), $hashPass);
				$messenger = $this->_helper->FlashMessenger;
				$messenger->addMessage('You successfully registered');
				$this->_helper->redirector('success', 'index');        		
        	}
        	else{
        		//form data is INVALID
        	}
        	
        }
        else{
        	//request is not POST
        }
    }
    public function logoutAction()
    {
    	Zend_Auth::getInstance()->clearIdentity();
        Zend_Session::destroy();
        $this->_redirect('/');
    }
}